'''
调试子界面类， 使用sub_debug.ui生成的布局
手动触发
'''
import enum
import sys
import os
import time
import traceback
from functools import partial

import unicodedata
from PyQt5 import QtWidgets

from PyQt5.QtWidgets import QApplication, QHeaderView, QWidget

# from ui.sub_ui.sub_debug import Ui_Form  # 界面控件， QtDesigner生成
from ui.client1.sub_ui.sub_debug1 import Ui_Form  # 界面控件， QtDesigner生成

from log import logger



class SubDebugWidget(QWidget, Ui_Form):

    '''

    usage:

    '''

    def __init__(self, parent=None, main_ui_handle=None):
        super(SubDebugWidget, self).__init__(parent)
        self.main_ui_handle = main_ui_handle
        if main_ui_handle is None:
            self.service_step_dict = {}
        else:
            self.service_step_dict = self.main_ui_handle.ui_config['runtime_service_step_dict']
        self.setupUi(self)
        # {
        #     'sname1': ['step1', 'step2'],
        #     'sname2': ['step1', 'step2', 'step3']
        # }

        # 额外样式
        self.setStyleSheet("""
                QPushButton {
                    min-width: 80px;
                    padding: 5px;
                    border-radius: 3px;
                    color: white;
                    background-color: dodgerblue;
                }
                QPushButton:hover {
                    background-color: darkblue;
                }
                
                /* 定义 QGroupBox 样式 */
                QGroupBox {
                    /*font-size: 14px;*/
                    font-weight: bold;
                    border: 1px solid #ccc;
                    border-radius: 8px; /* 使用像素值代替分数 */
                    padding: 1em;
                    margin-top: 1em;
                    background-color: #f5f5f5;
                }
                
                /* 设置标题样式 */
                QGroupBox::title {
                    subcontrol-origin: margin;
                    /*subcontrol-position: top center;*/
                    padding: 0 1em;
                    color: #333;
                    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                                    stop: 0 #f5f5f5, stop: 1 #e5e5e5);
                    border-bottom: 1px solid #ccc;
                    border-top-left-radius: 8px;
                    border-top-right-radius: 8px;
                }
                
                /* 输入框样式 */
                QLineEdit {
                    border: 1px solid #ccc; /* 边框颜色 */
                    border-radius: 4px; /* 边框圆角 */
                    background-color: #fff; /* 背景颜色 */
                }
                /* 输入框焦点样式 */
                QLineEdit:focus {
                    border-color: #80bfff; /* 焦点边框颜色 */
                }
            """)

        # self.pushButton_3.clicked.connect(partial(self.munal_trigger_solt, 1))
        # self.pushButton_4.clicked.connect(partial(self.munal_trigger_solt, 2))
        # self.pushButton_5.clicked.connect(partial(self.munal_trigger_solt, 3))
        # self.pushButton_6.clicked.connect(partial(self.munal_trigger_solt, 4))
        # self.pushButton_7.clicked.connect(partial(self.munal_trigger_solt, 5))  #

        for sname_str, steps_list in self.service_step_dict.items():
            groupbox = self.findChild(QtWidgets.QGroupBox, sname_str)
            for step_str in steps_list:
                btn = groupbox.findChild(QtWidgets.QPushButton, step_str)
                btn.clicked.connect(partial(self.munal_trigger_solt, sname_str, step_str))  #

    def munal_trigger_solt(self, sname_str, step_str):

        try:
            print(f'manual_trigger_slot {sname_str} {step_str}...')
            # plc编码策略相关
            step_value = 0
            for ind, s in enumerate(self.service_step_dict[sname_str]):
                # step_str = unicodedata.normalize('NFC', step_str)
                # s = unicodedata.normalize('NFC', s)
                # print(f'step_str {step_str} s {s}')
                if step_str == s:
                    step_value = ind + 1
                    break
            print(f'step_value: {step_value}')
            self.main_ui_handle.ss.plcMain.set_manual_trigger(var_name_trigger=f'{sname_str}.is_jc',
                                                              var_name_step_mode=f'{sname_str}.step',
                                               values=[1, step_value])
        except:
            print(traceback.format_exc())





if __name__ == '__main__':
    try:
        app = QtWidgets.QApplication(sys.argv)
        mainWnd = SubDebugWidget()
        # ui = Ui_MainWindow()
        # ui.setupUi(mainWnd)
        mainWnd.show()
        sys.exit(app.exec())
    except:
        print(traceback.format_exc())
        logger.error(traceback.format_exc())